package com.ooqiu.gaming.ui.person.collect;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.GamingApi;
import com.ooqiu.gaming.api.entities.Article;
import com.ooqiu.gaming.base.BaseFragment;
import com.ooqiu.gaming.components.retrofit.progress.ProgressSubscriber;
import com.ooqiu.gaming.ui.article_detail.ArticleDetailActivity;
import com.ooqiu.gaming.ui.article_detail.VideoDetailActivity;
import com.ooqiu.gaming.ui.main.thread.index.content.IndexContentAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */

@Route(path = CollectFragment.PATH)
public class CollectFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    public static final String PATH = "/fragment/person/collect";

    @Autowired
    int type;
    @BindView(R.id.rv_collect_list)
    RecyclerView mRvCollectList;
    @BindView(R.id.swiper)
    SwipeRefreshLayout mSwiper;
    Unbinder unbinder;
    List<Article> mArticles = new ArrayList<>();
    IndexContentAdapter mIndexContentAdapter;
    private int page = 1;

    private boolean isRefresh;

    private GamingApi mGamingApi;


    @Override
    public void initInjector(View view) {
        unbinder = ButterKnife.bind(this, view);
        mGamingApi = getApplicationComponent().getGamingApi();
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_collect;
    }

    @Override
    public void initUI(View view) {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


        mSwiper.setOnRefreshListener(this);
        mRvCollectList.setLayoutManager(new LinearLayoutManager(mContext));
        mRvCollectList.setAdapter(mIndexContentAdapter = new IndexContentAdapter(mArticles));

        mIndexContentAdapter.setEnableLoadMore(true);
        mIndexContentAdapter.setOnLoadMoreListener(this, mRvCollectList);

        mIndexContentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Article article = (Article) adapter.getItem(position);
                if (article.getContent_type() == 3) {
                    getRouter().build(VideoDetailActivity.PATH)
                            .withInt("news_id", article.getNews_id())
                            .navigation();
                } else {
                    getRouter().build(ArticleDetailActivity.PATH)
                            .withInt("news_id", article.getNews_id())
                            .navigation();
                }

            }
        });

        onRefresh();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        mSwiper.setRefreshing(true);
        page = 1;
        isRefresh = true;
        fetchCollect();
    }

    @Override
    public void onLoadMoreRequested() {
        fetchCollect();
    }


    private void fetchCollect() {
        mGamingApi.fetchCollect(type, page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<Article>>(mContext) {

                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onNext(List<Article> articles) {
                        if (isRefresh) {
                            mArticles.clear();
                            isRefresh = false;
                        }
                        mArticles.addAll(articles);

                        if (articles.isEmpty()) {
                            mIndexContentAdapter.loadMoreEnd(true);
                        } else {
                            mIndexContentAdapter.loadMoreComplete();
                        }
                        page++;
                        mIndexContentAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {
                        mSwiper.setRefreshing(false);
                    }
                });
    }

}
